// 从浏览器的地址栏中获取查询参数
function getUrlParams(name) {
    var paramsAry = location.search.substr(1).split('&');
    // 循环数据
    for (var i = 0; i < paramsAry.length; i++) {
        var tmp = paramsAry[i].split('=');
        if (tmp[0] == name) {
            return tmp[1];
        }
    }
    return -1;
}


$(function () {
    var id = getUrlParams('id')

    if (id != -1) {
        $.ajax({
            url: `/posts/${id}`,
            success: function (res) {
                console.log(res, 7777);

                // var html = template('post-add', res)
                // $('.col-md-9').html(html)

                /* 获取分类并渲染 */
                $.ajax({
                    url: '/categories',
                    success: function (cate) {
                        res.categorys=cate
                        console.log(res, 666);
                         var html = template('tpl-cate', res)
                        // $('.col-md-3').html(html)
                        $('.container-fluid').html(html)
                    }
                })
            }
        })
    }


    /* 修改文章分类的数据 */
    $('.container-fluid').on('submit','form',function(){
        var  data=$(this).serialize()
        var id = getUrlParams('id')
        console.log(data,567);
        $.ajax({
            url:'/posts/'+id,
            type:'put',
            data:data,
            success:function(res){
                location.href='/admin/posts.html'
            }

        })

        return  false;
    })




})